﻿using System;
using System.Collections.Generic;

namespace StockTradTest.Data
{
    public class CalcIndicator
    {
        //计算Ma

        public static void CalcAllMaForCode(List<Stock> stocks,out List<Indicator> dataList)
        {
            ////最旧-最新
            int count; double ma;
            List<int> nList = new List<int> { 5, 10, 20, 30, 60, 120, 250 };
            dataList = new List<Indicator>();
            for(int i = 0; i< stocks.Count;i++)
            {
                Indicator indicator = new Indicator
                {
                    Date = stocks[i].Date
                };
                dataList.Add(indicator);
            }
            foreach (int n in nList)
            {
                count = 0;
                for(int i = 0; i< stocks.Count;i++)
                {


                    if (count <= n)
                    {
                        ma = stocks[i].ClosePrice;
                    }
                    else
                    {
                        double result = 0;
                        for (int j = 0; j < n; j++)
                        {
                            result += stocks[count - j].ClosePrice;
                        }
                        ma = result / n;
                    }
                    switch (n)
                    {
                        case 5: dataList[i].Ma5 = ma; break;
                        case 10: dataList[i].Ma10 = ma; break;
                        case 20: dataList[i].Ma20 = ma; break;
                        case 30: dataList[i].Ma30 = ma; break;
                        case 60: dataList[i].Ma60 = ma; break;
                        case 120: dataList[i].Ma120 = ma; break;
                        case 250: dataList[i].Ma250 = ma; break;
                        default: dataList[i].Ma = ma; break;
                    }
                    count++;
                }
            }
        }

        //计算Ma

        public static void CalcAllMaForCode(List<Index> stocks, out List<Indicator> dataList)
        {
            ////最旧-最新
            int count; double ma;
            List<int> nList = new List<int> { 5, 10, 20, 30, 60, 120, 250 };
            dataList = new List<Indicator>();
            for (int i = 0; i < stocks.Count; i++)
            {
                Indicator indicator = new Indicator
                {
                    Date = stocks[i].Date
                };
                dataList.Add(indicator);
            }
            foreach (int n in nList)
            {
                count = 0;
                for (int i = 0; i < stocks.Count; i++)
                {


                    if (count <= n)
                    {
                        ma = stocks[i].ClosePrice;
                    }
                    else
                    {
                        double result = 0;
                        for (int j = 0; j < n; j++)
                        {
                            result += stocks[count - j].ClosePrice;
                        }
                        ma = result / n;
                    }
                    switch (n)
                    {
                        case 5: dataList[i].Ma5 = ma; break;
                        case 10: dataList[i].Ma10 = ma; break;
                        case 20: dataList[i].Ma20 = ma; break;
                        case 30: dataList[i].Ma30 = ma; break;
                        case 60: dataList[i].Ma60 = ma; break;
                        case 120: dataList[i].Ma120 = ma; break;
                        case 250: dataList[i].Ma250 = ma; break;
                        default: dataList[i].Ma = ma; break;
                    }
                    count++;
                }
            }
        }
        //计算布林线
        public static void CalcBoll(int n, List<Stock> stocks, ref List<Indicator> dataList)
        {
            if (dataList == null)
            {
                dataList = new List<Indicator>();
                for (int i = 0; i < stocks.Count; i++)
                {
                    Indicator indicator = new Indicator
                    {
                        Date = stocks[i].Date
                    };
                    dataList.Add(indicator);
                }
            }
            double maX; double md;
            for (int i = 0; i < stocks.Count; i++)
            {
                maX = 0; md = 0;
                dataList[i].Date = stocks[i].Date;
                if (i <= n)
                {
                    dataList[i].BollUp = stocks[i].HighPrice;
                    dataList[i].BollDown = stocks[i].LowPrice;
                    dataList[i].BollMd = stocks[i].ClosePrice;
                }
                else
                {
                    for (int j = 0; j < n; j++)
                    {
                        maX += stocks[i - j].ClosePrice;
                    }
                    maX /= n;
                    for (int j = 0; j < n; j++)
                    {
                        md += Math.Pow(stocks[i - j].ClosePrice - maX, 2);
                    }
                    md = Math.Sqrt(md / n);
                    dataList[i].BollUp = maX + 2 * md;
                    dataList[i].BollMd = maX;
                    dataList[i].BollDown = maX - 2 * md;
                    dataList[i].Width = 4 * md / maX;
                }
            }
        }
        //计算指数布林线
        public static void CalcBoll(int n, List<Index> indexs, ref List<Indicator> dataList)
        {
            if (dataList == null)
            {
                dataList = new List<Indicator>();
                for (int i = 0; i < indexs.Count; i++)
                {
                    Indicator indicator = new Indicator
                    {
                        Date = indexs[i].Date
                    };
                    dataList.Add(indicator);
                }
            }
            double maX; double md;
            for (int i = 0; i < indexs.Count; i++)
            {
                maX = 0; md = 0;
                dataList[i].Date = indexs[i].Date;
                if (i <= n)
                {
                    dataList[i].BollUp = indexs[i].HighPrice;
                    dataList[i].BollDown = indexs[i].LowPrice;
                    dataList[i].BollMd = indexs[i].ClosePrice;
                }
                else
                {
                    for (int j = 0; j < n; j++)
                    {
                        maX += indexs[i - j].ClosePrice;
                    }
                    maX /= n;
                    for (int j = 0; j < n; j++)
                    {
                        md += Math.Pow(indexs[i - j].ClosePrice - maX, 2);
                    }
                    md = Math.Sqrt(md / n);
                    dataList[i].BollUp = maX + 2 * md;
                    dataList[i].BollMd = maX;
                    dataList[i].BollDown = maX - 2 * md;
                }
            }
        }
    }
}
